package com.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * servlet
 * 开发者不会去new MyFirstServlet
 */
public class RegisterServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        doPost(request,response);
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Connection conn = null;   Statement stmt;   ResultSet rs;
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String gender = request.getParameter("gender");
        String email = request.getParameter("email");
        String telephone = request.getParameter("telephone");
        String introduce = request.getParameter("introduce");
        gender = new String(gender.getBytes("ISO8859-1"),"UTF-8");
        introduce = new String(introduce.getBytes("ISO8859-1"),"UTF-8");
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn= DriverManager.getConnection(
                    "jdbc:mysql://localhost:3308/bookstore?useUnicode=true&characterEncoding=utf8","root","123456");
            stmt = conn.createStatement();
            //stmt.executeUpdate("insert into users values("+username+","+password+","+gender+","+email+","+telephone+","+introduce+")");
            stmt.executeUpdate("insert into users values('"+username+"','"+password+"','"+gender+"','"+email+"','"+telephone+"','"+introduce+"')");
            response.setContentType("text/html;charset=utf-8");
            response.sendRedirect("/2_war_exploded/registersuccess.html");
            stmt.close();
            conn.close();
        } catch (Exception e)
        {
            response.setContentType("text/html;charset=utf-8");
            response.getWriter().write("<h1>注册失败</h1>");
            System.out.println(e);
        }
    }
}
